/**
 * 登录，注册
 */
define(function(require, exports, module){
	var $ = require("jquery");
	require("lib/jquery/jquery.validate.min.js");
	require("lib/common/CommonUtil.js");
	require("lib/common/common.js");
	
	var type = CommonUtil.getRequestParam("type");
	if(type === 'login'){
		$('.form-panel').addClass('hide');
		$('.form-login').removeClass('hide');
	} else if(type === 'reg'){
		$('.form-panel').addClass('hide');
		$('.form-register').removeClass('hide');
	}
	
	var error = {
		'10004': '服务器异常!',
		'10008': '验证码不正确!',
		'10006': '账号或密码不正确!',
		'10007': '账号被冻结!',
		'10019': '动态密码已失效!',
		'10021': '密码不正确!',
		'10022': '账号不存在!',
		'10023': '账号存在异常，禁止登陆!'
	};
	
	$.validator.addMethod('emailOrPhone', function(e, a){
		var phoneExc = /^(13|14|15|18)[0-9][0-9]{8}$/;
		var emailExc = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
		return phoneExc.test(e) || emailExc.test(e);
	});
	
	$("#loginForm").validate({
    	rules: {
    	   	account: {
    	    	required: true,
    	    	emailOrPhone: true
    	   	},
    	   	password: {
    	    	required: true,
    	    	minlength: 5
    	   	}
	  	},
    	messages: {
    		account: {
    	    	required: "请输入Email地址或手机号码",
    	    	emailOrPhone: "请输入正确的email地址或手机号码"
    	   	},
    	   	password: {
    	   		required: "请输入登录密码",
    	    	minlength: jQuery.format("密码不能小于{0}个字 符")
    	   	}
		}
    });
	
	$("#registerForm").validate({
    	rules: {
    		nickname: "required",
			account: {
    	    	required: true,
    	    	emailOrPhone: true
    	   	},
    	   	password: {
    	    	required: true,
    	    	minlength: 5
    	   	},
    	   	repassword: {
    	    	required: true,
    	    	minlength: 5
    	   	}
	  	},
    	messages: {
    		nickname: "请输入姓名或昵称",
    		account: {
    	    	required: "请输入Email地址或手机号码",
    	    	emailOrPhone: "请输入正确的email地址或手机号码"
    	   	},
    	   	password: {
    	   		required: "请输入密码",
    	    	minlength: jQuery.format("密码不能小于{0}个字 符")
    	   	},
    	   	repassword: {
    	   		required: "请重复输入密码",
    	    	minlength: jQuery.format("密码不能小于{0}个字 符")
    	   	}
		}
    });
	
	var accountCheck = function(){
		
	};
	
	$('#account').blur(accountCheck);
	
	var repassfunc = function(){
		var $pass = $('#repassword'), error = '';
		if($pass.val() == ''){
			error = '请重复输入密码';
		} else {
			if($pass.val() != $('#registerForm #password').val()){
				error = '两次密码输入不一致';
			}
		}
		if(error == ''){
			if($pass.next().attr('for')){
				$pass.next().css('display', 'none');
			}
		} else {
			if($pass.next().attr('for')){
				$pass.next().text(error);
				$pass.next().css('display', 'inline-block');
			} else {
				$pass.after('<label for="repassword" class="error" style="display: inline-block;">'+error+'</label>');
			}
		}
	};
	
	$('#repassword').blur(repassfunc);
	
	$('.form-input input').on('focus', function(){
		$(this).parent().parent().css({'border': '1px double #F49B74'});
	});
	
	$('.form-input input').on('blur', function(){
		$(this).parent().parent().css({'border': '1px double #EFEFEF'});
	});
	
	$('#remenberPwd').on('click', function(){
		$(this).attr('checked', $(this).attr('checked') == 'checked' ? false: true);
	});
	
	$('#btn_login').click(function(){
		var $form = $("#loginForm");
	    if($form.valid()){
	    	var ida = top.data.dialog({cancel: false, escClose: false}).title('登录中...').showModal();
	    	$('#errorInfo').html('');
	    	var remenberPwd = $('#remenberPwd').attr('checked') == 'checked';
	    	$.ajax({
	    		url: top.data.ctx + '/frame/open',
	    		data: $form.serialize(),
	    		type: 'post',
	    		success: function(){
	    			try{
		    			if(!error[arguments[0]]){
		    				$('#errorInfo').css('color', 'green').html('恭喜！登录成功！');
							CommonUtil.addCookie("acc", $('#account').val(), 30);
		    				if(remenberPwd){
								CommonUtil.addCookie("pwd", $('#password').val(), 30);
		    				} else {
								CommonUtil.deleteCookie("pwd");
		    				}
		    				loadUserData($('#account').val());
		    			} else {
		    				$('#errorInfo').css('color', 'red').html(error[arguments[0]]);
		    			}
		    			ida.remove();
	    			}catch(e){
	    				console.info(e);
	    			}
	    		},
	    		error: function(){
	    			ida.remove();
	    		}
	    	});
	    }
	});
	
	$('#btn_reg').click(function(){
		var $form = $("#registerForm");
	    if($form.valid()){
	    	var ida = top.data.dialog({cancel: false, escClose: false}).title('处理中...').showModal();
	    	$('#errorInfo').html('');
	    	$.ajax({
	    		url: top.data.ctx + '/system/usersAction!insertData',
	    		data: $form.serialize(),
	    		type: 'post',
	    		success: function(){
	    			if(!error[arguments[0]]){
	    				$('#errorInfo').css('color', 'green').html('恭喜！注册成功！');
						CommonUtil.addCookie("acc", $('#account').val(), 30);
						$('#login-href').click();
	    			} else {
	    				$('#errorInfo').css('color', 'red').html(error[arguments[0]]);
	    			}
	    			ida.remove();
	    		},
	    		error: function(){
	    			ida.remove();
	    		}
	    	});
	    }
	});
	
	$('#login-href').on('click', function(){
		top.data.dialog.get('reg-dialog').close();
		top.data.dialog.get('login-dialog').showModal();
	});
	
	$('#reg-href').on('click', function(){
		var logdia = top.data.dialog.get('login-dialog');
		top.data.loginDialog = top.data.dialog({
			id: 'reg-dialog',
			title: '注册',
			page: {url: top.data.ctx + '/pages/login.html?type=reg', func: function(){
				logdia.close();
			}},
			width: 380, height: 370, 
			okDisplay: false
		}).showModal();
		//console.info(logdia.id, top.data.loginDialog.id);
	});
	
	$('#zhmm-href1,zhmm-href2').on('click', function(){
		
	});
	
	var loadUserData = function(acc) {
		closeDialog();
		Common.ajax({
			url: top.data.ctx + '/system/usersAction!selectDataForAccurate',
			param: 'account=' + acc,
			success: function(d, jd){
				if(jd.size > 0 && jd.data.length > 0){
					var u = jd.data[0];
					top.data.user = u;
					var uhtml = '<div>' + (u.name==''? u.nickname : u.name) + '</div><div>' + u.account + '</div>';
					$('.user-name', parent.document).html(uhtml);
					$('.ficon.login', parent.document).removeClass('login').addClass('exit').parent().addClass('login');
					
					if(top.data.pageType == "edit"){
						top.edit.WebProject.load_project();
						top.edit.CodeFile.load_file();
						top.data.dialog.get('login-dialog').remove();
					} else if(top.data.pageType == "index"){
						top.index.userLogined();
					}
				}
			}
		});
	};
	
	var closeDialog = function(){
		try{
			top.data.loginDialog && top.data.loginDialog.close();
		}catch(e){
			console.info(2222);
		}
	};
	
	var removeDialog = function(aria){
		try{
			var $div = $('div[aria-labelledby="'+aria+'"]');
			$div.prev().remove();
			$div.next().remove();
			$div.remove();
		}catch(e){
			console.info(2222);
		}
	};
	
	var acc = CommonUtil.getCookie("acc");
	var pwd = CommonUtil.getCookie("pwd");
	if(acc != null && acc != ''){
		$('#account').val(acc);
	}
	if(pwd != null && pwd != ''){
		$('#password').val(pwd);
		$('#remenberPwd').get(0).checked = true;
	}
	
});